<?php

class Model_Vote_User extends PhalApi_Model_NotORM {

    public function  lists($where,  $orderby = "poll desc") {
        return $this->getORM()
            ->select("id,realname as name,poll,no,headimgurl")
            ->where($where)
            ->order($orderby)
            ->fetchAll();
    }


    public function items() {
        $resultDB = $this->getORM()
            ->select("item")
            ->group("item")
            ->fetchAll();
        foreach ($resultDB as $r) {
            $result[] = $r["item"];
        }
        return $result;
    }

    public function deleteByNameAndItem($id, $itemid) {
        return $this->getORM()
            ->where(["id" => $id])
            ->where(["item" => $itemid])
            ->delete();
    }

    public function add($data) {
        return $this->getORM()
            ->insert($data);

    }
    public function edit($data,$id) {
        return $this->getORM()
            ->where('id', $id)
            ->update($data);
    }
    public function setpoll($poll,$NO,$item,$name) {
        return $this->getORM()
            ->where('no', $NO)
            ->where('item', $item)
            ->update([
                'realname'=>$name,
                'poll'=>$poll
            ]);
    }

    public function plusPoll($id,$item){
        return $this->getORM()
            ->where('id', $id)
            ->where('item', $item)
            ->update([
                'poll'=>new NotORM_Literal("poll + 1")
            ]);
    }


    public function hasNameInItem($id,$item) {
        return $this->getORM()
            ->where(["id" => $id])
            ->where(["item" => $item])
            ->count("id");
    }
    public function hasName($NO) {
        return $this->getORM()
            ->where(["id" => $NO])
            ->count("id");
    }
    public function info($id) {
        return $this->getORM()
            ->select("id,realname as name,poll,no as NO,headimgurl")
            ->where(["id" => $id])
            ->fetch();
    }
    protected function getTableName($id) {
        return 'votes_Users';
    }
}
